"""

@author: jiangpj
@email: pj.jiang@hzgosun.com
@file: securityManageTest.py
@time: 2019/5/31 11:57

保安管理的增删改查自动化测试用例编写

"""
import os
import unittest, random
from time import sleep
from selenium.webdriver.common.keys import Keys
from common.decorators import log4gs, mkdir_classname
from common.functions import save_screenshot
from common.settings import DATA_DIR, TAG
from common.vue.select import DropDownSelect, OperateSelect
from pages.securityManagePage import securityPage


@log4gs
@mkdir_classname(TAG)
class SecurityTest(unittest.TestCase):
    """保安管理增删改查功能测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.debug('=' * 30 + '开始保安管理功能测试' + '=' * 30)
        cls.page = securityPage('/CmddManage/securityManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束保安管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        sleep(2)
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    # @unittest.skip
    def test_1_page(self):
        """保安管理翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        if int(self.page.turnPage.total_page) >= 3:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')
            sleep(2)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(3)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '3')
        else:
            self.log.error("当前模块最大未到3页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    # @unittest.skip
    def test_2_1_add(self):
        """增加保安信息用例"""
        name = "江测试自动"
        self.page.table.add_btn.click()
        sleep(1)
        self.page.addsecurity.name.send_keys(name)
        sleep(1)
        self.page.addsecurity.face_pic.send_keys(os.path.join(DATA_DIR, "测试人脸1.jpg"))
        DropDownSelect(self.page.addsecurity.duty, "经理")
        sleep(1)
        DropDownSelect(self.page.addsecurity.state, "在职")
        sleep(1)
        self.page.addsecurity.id_.send_keys('330391199905031000')
        sleep(1)
        self.page.addsecurity.residential.send_keys('郑州')
        sleep(1)
        self.page.addsecurity.contact_way.send_keys('18816784399')
        self.page.addsecurity.emergency_contact.send_keys('18816784399')
        sleep(1)
        self.page.addsecurity.confirm.click()
        sleep(1)
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, '姓名')
        self.page.searchTools.keyword_input.send_keys(name)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        result = self.page.table.get_row(name)
        try:
            self.assertTrue(result, "找到{}".format(name))
            self.log.info("用例增加{}保安执行成功".format(name))
        except Exception:
            self.log.error("用例增加{}保安执行失败,".format(name), exc_info=True)

    # @unittest.skip
    def test_3_1_alter(self):
        """修改保安信息管理用例"""
        name = '江测试自动'
        self.page.searchTools.keyword_input.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        result = self.page.table.get_row(name)
        OperateSelect(result, "修改")
        sleep(1)
        position = ['经理', '大队长', '队长', '班长', '队员']
        duty = random.choice(position)
        DropDownSelect(self.page.modifysecurity.duty, duty)
        sleep(1)
        DropDownSelect(self.page.modifysecurity.state, "离职")
        sleep(1)
        self.page.modifysecurity.confirm.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        result = self.page.table.get_row(duty)
        dimission = self.page.table.get_row("离职")
        try:
            self.assertTrue(result, "职务找到{}".format(duty))
            self.assertTrue(dimission, "状态找到离职")
            self.log.info("用例修改{}保安执行成功".format(name))
        except Exception:
            self.log.error("用例修改{}保安执行失败,".format(name), exc_info=True)

    # @unittest.skip
    def test_3_2_remove(self):
        """删除保安信息管理用例"""
        name = '江测试自动'
        self.page.searchTools.keyword_input.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        result = self.page.table.get_row(name)
        OperateSelect(result, "删除")
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        rm_result = self.page.table.get_row(name)
        sleep(1)
        try:
            self.assertFalse(rm_result)
            self.log.info("用例删除{}保安执行成功".format(name))
        except:
            self.log.error("用例删除{}保安执行失败,".format(name), exc_info=True)

    # @unittest.skip
    def test_4_1_antistop_search(self):
        """保安信息关键词搜索"""
        search_list = [
            {
                'condition': "姓名",
                'input': "我的",
                'expectation': '我的'
            },
            {
                'condition': "家庭住址",
                'input': "法法师",
                'expectation': '包旗测试保安'

            },
            {
                'condition': "联系方式",
                'input': "18039314010",
                'expectation': '小鹿'
            }
        ]
        for item in search_list:
            condition = item.get('condition')
            input_ = item.get('input')
            expectation = item.get('expectation')
            with self.subTest(**item):
                self.log.info("用以下数据进行搜索测试：\n{}".format(item))
                DropDownSelect(self.page.searchTools.keyword_dropdown_list, condition)  # 关键字选择姓名
                self.page.searchTools.keyword_input.clear()
                self.page.searchTools.keyword_input.send_keys(input_)  # 关键字
                self.page.searchTools.search_btn.click()  # 点击检索按钮
                sleep(1)
                result = self.page.table.get_row(expectation)
                self.page.searchTools.keyword_input.clear()
                sleep(1)
                try:
                    self.assertTrue(result, msg="没有找到结果：{}".format(expectation))
                    self.log.info("用例关键词：{}搜索执行成功".format(condition))
                except Exception:
                    self.log.error("用例关键词：{}搜索执行失败,".format(condition), exc_info=True)
                sleep(1)

    def test_4_2_area_search(self):
        """保安信息区域检索"""
        area = "河南省-郑州市-二七区"
        name = "江江"
        self.page.searchTools.region_region_input.click()
        sleep(1)
        self.page.searchTools.region_region_selector.select(area)
        sleep(1)
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        result = self.page.table.get_row(name)
        self.page.searchTools.region_region_input.clear()
        try:
            self.assertTrue(result, msg="没有找到结果：{}".format(name))
            self.log.info("用例区域{}执行成功".format(area))
        except Exception:
            self.log.error("用例区域{}执行失败,".format(area), exc_info=True)
        sleep(1)

    def test_4_3_status_search(self):
        """保安信息状态查询"""
        state = "离职"
        DropDownSelect(self.page.searchTools.state_list, state)
        sleep(1)
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        result = self.page.table.get_row(state)
        self.page.searchTools.state_list.clear()
        try:
            self.assertTrue(result, msg="没有找到结果：{}".format(state))
            self.log.info("用例区域搜索到{}执行成功".format(state))
        except Exception:
            self.log.error("用例区域搜索到{}执行失败,".format(state), exc_info=True)
        sleep(1)

    def test_4_4_duty_search(self):
        """保安信息职务查询"""
        duty = "经理"
        DropDownSelect(self.page.searchTools.duty_list, duty)
        sleep(1)
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        result = self.page.table.get_row(duty)
        self.page.searchTools.duty_list.clear()
        try:
            self.assertTrue(result, msg="没有找到结果：{}".format(duty))
            self.log.info("用例区域搜索到{}执行成功".format(duty))
        except Exception:
            self.log.error("用例区域搜索到{}执行失败".format(duty), exc_info=True)

    # @unittest.skip
    def test_5_Invento(self):
        """导入表格"""
        name = "表格自动测试"
        self.page.table.to_btn.click()
        sleep(1)
        self.page.invento.to_lead.send_keys(os.path.join(DATA_DIR, "保安信息.xlsx"))
        sleep(1)
        self.page.invento.upload.click()
        sleep(1)
        self.page.invento.close()
        sleep(1)
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, '姓名')
        self.page.searchTools.keyword_input.send_keys(name)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        result = self.page.table.get_row(name)
        try:
            self.assertTrue(result, f"找到{name}")
            self.log.info("用例导入表格执行成功")
        except Exception:
            self.log.error("用例导入表格执行失败,", exc_info=True)


if __name__ == '__main__':
    unittest.main()
